#include "vcard.h"



typedef struct 
{
    /* data */
    const char *info;
    const void *img;
}infor_t;

LV_IMG_DECLARE(img_name_64);
LV_IMG_DECLARE(img_baby_64);
LV_IMG_DECLARE(img_chat_64);
LV_IMG_DECLARE(img_factory_64);
LV_IMG_DECLARE(img_garage_64);
LV_IMG_DECLARE(img_school_64);

//https://www.biography.com/business-figure/steve-jobs
static infor_t infor_steven_jobs[]={
    {"Steven\nJobs",&img_name_64},
    {"Adopted",&img_baby_64},
    {"College\ndropout",&img_school_64},
    {"Started\nApple\nin garage",&img_garage_64},
    {"worked at\nAtari",&img_factory_64},
    {"Last words:\n\"Oh wow.\"",&img_chat_64},

};

static lv_obj_t *quickfacts_img;
static lv_obj_t *quickfacts;

static lv_task_t * task;

static void info_task(lv_task_t * task);
static void btn_back_event_cb(lv_obj_t * btn, lv_event_t event);
void vcard_create(lv_obj_t *obj, lv_event_t event)
{
	 
	if(event==LV_EVENT_CLICKED)
    {
        

        //eink
        eink.setTextColor(GxEPD_BLACK);
        eink.setFullWindow();
        eink.firstPage();
        do
        {
            eink.fillScreen(GxEPD_WHITE);        
        }
        while (eink.nextPage());


        lv_obj_t *scr = lv_obj_create(scr_eink,NULL);
        lv_obj_set_size(scr,176,264);

        //logo
        LV_IMG_DECLARE(img_jobs_128);
         
        lv_obj_t *img_logo = lv_img_create(scr,NULL);
        // lv_obj_set_size(img_logo,100,100);
        lv_img_set_src(img_logo,&img_jobs_128);

        lv_obj_align(img_logo,scr,LV_ALIGN_IN_TOP_MID,0,0);




        static lv_style_t style_big,style_mid,style_little;

        lv_style_copy(&style_big,&lv_style_plain);
        style_big.text.font = &lv_font_roboto_22;

        lv_style_copy(&style_mid,&lv_style_plain);
        style_mid.text.font = &lv_font_roboto_12;

        lv_style_copy(&style_little,&lv_style_plain);
        style_little.text.font = &lv_font_unscii_8; 

        //name
        // lv_obj_t *name = lv_label_create(scr,NULL);
        // lv_label_set_text(name,"Steven Jobs");
        // lv_label_set_style(name,LV_LABEL_STYLE_MAIN,&style_big);
        // lv_obj_align(name,img_logo,LV_ALIGN_OUT_BOTTOM_MID,0,0);
         

        //
        lv_obj_t *title_age = lv_label_create(scr,NULL);
        lv_label_set_text(title_age,"TECH ENTREPRENEUR\n1955-2011");
        lv_label_set_align(title_age,LV_LABEL_ALIGN_RIGHT);
        lv_label_set_style(title_age,LV_LABEL_STYLE_MAIN,&style_mid);


        lv_obj_t *infor = lv_label_create(scr,NULL);
        lv_label_set_long_mode(infor, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
        // lv_label_set_text(infor,"Steve Jobs co-founded Apple Computers with Steve Wozniak in1976."
        //                          "He left in1985 to launch Pixar Animation Studios,but returned as"
        //                          "CEO in 1997.Under his reign, Apple launched the iPod and iPhone."
        //                          "Jobs died in 2011 of pancreatic cancer.");

        lv_label_set_text(infor,//"I like to believe there's an afterlife."
                                "I like to believe the accumulated wisdom"
                                "dosen't just disappear when you die,"
                                "but somehow it endures.");
        lv_obj_set_width(infor, 176-6);
        lv_label_set_style(infor,LV_LABEL_STYLE_MAIN,&style_little);

        lv_obj_align(title_age,scr,LV_ALIGN_IN_TOP_RIGHT,-10,128);
        lv_obj_align(infor,scr,LV_ALIGN_IN_BOTTOM_MID,0,-40);

        //back
        lv_obj_t *btn_back = lv_btn_create(scr,NULL);
        lv_obj_set_size(btn_back,40,40);
        static lv_style_t style_btn;
        lv_style_copy(&style_btn,&lv_style_transp);
        lv_btn_set_style(btn_back,LV_BTN_STYLE_REL,&style_btn);
        lv_btn_set_style(btn_back,LV_BTN_STYLE_PR,&style_btn);

        lv_obj_t *btn_left = lv_btn_create(scr,btn_back);
        lv_obj_t *btn_right = lv_btn_create(scr,btn_back);

        lv_obj_t *label_back = lv_label_create(btn_back,NULL);
        lv_obj_t *label_left = lv_label_create(btn_left,NULL);
        lv_obj_t *label_right = lv_label_create(btn_right,NULL);

        lv_label_set_text(label_back,LV_SYMBOL_CLOSE); 
        lv_label_set_text(label_left,LV_SYMBOL_LEFT); 
        lv_label_set_text(label_right,LV_SYMBOL_RIGHT); 

        
        lv_obj_align(btn_left,scr,LV_ALIGN_IN_BOTTOM_MID,0,0);   
        lv_obj_align(btn_back,btn_left,LV_ALIGN_OUT_LEFT_MID,-10,0);
        lv_obj_align(btn_right,btn_left,LV_ALIGN_OUT_RIGHT_MID,10,0);
        
        lv_obj_set_event_cb(btn_back, btn_back_event_cb);  


        eink_scene_ready=false;
        eink_flash_ready=false;

        //tft
        lv_label_set_text(label_menu,"");
        lv_obj_align(label_menu,scr_tft,LV_ALIGN_CENTER,0,0);

        // lv_obj_t *page = lv_page_create(scr_tft,NULL);
        // lv_obj_set_size(page,160,80);
        

              

        quickfacts_img = lv_img_create(scr_tft,NULL);
        lv_img_set_src(quickfacts_img,infor_steven_jobs[0].img); 
        // lv_obj_set_size(quickfacts_img,64,64);

        quickfacts = lv_label_create(scr_tft,NULL);
        // lv_label_set_long_mode(quickfacts, LV_LABEL_LONG_BREAK); 
        lv_label_set_align(quickfacts,LV_LABEL_ALIGN_CENTER);
        lv_label_set_text(quickfacts,infor_steven_jobs[0].info);   

        lv_obj_align(quickfacts_img,scr_tft,LV_ALIGN_IN_LEFT_MID,0,0);
        lv_obj_align(quickfacts,quickfacts_img,LV_ALIGN_OUT_RIGHT_MID,5,0);



        



        static uint32_t user_data = 10;
        task = lv_task_create(info_task, 3000, LV_TASK_PRIO_MID, &user_data);
        

    }




}

static void btn_back_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) {

        lv_task_del(task);
        
        lv_obj_t * par = lv_obj_get_parent(btn);
        lv_obj_del(par);

        lv_obj_del(quickfacts);
        lv_obj_del(quickfacts_img);

        lv_label_set_text(label_menu,"MENU");
        lv_obj_align(label_menu,scr_tft,LV_ALIGN_CENTER, 0 ,0);

        eink_scene_ready=false;
        eink_flash_ready=false;



    }
}

static void info_task(lv_task_t * task)
{
  
  
  static uint8_t i=0;
  
           
        // lv_label_set_long_mode(quickfacts, LV_LABEL_LONG_BREAK); 
        // lv_label_set_align(quickfacts,LV_LABEL_ALIGN_CENTER);
        lv_label_set_text(quickfacts,infor_steven_jobs[i].info);         

        
        lv_img_set_src(quickfacts_img,infor_steven_jobs[i].img); 
        // lv_obj_set_size(quickfacts_img,64,64);

        lv_obj_align(quickfacts_img,scr_tft,LV_ALIGN_IN_LEFT_MID,0,0);
        lv_obj_align(quickfacts,quickfacts_img,LV_ALIGN_OUT_RIGHT_MID,5,0);

    i++;
    if(i==6)
    {
        i=0;
    }

//   /*Do something with LittlevGL*/
//   if(something_happened) {
//     something_happened = false;
//     lv_btn_create(lv_scr_act(), NULL);
//   }
}

 

